package validate

import (
	"github.com/go-playground/validator/v10"
	"go-project/dto/requrest"
	"go-project/models"
	"log"
)

func PermissionsNameUniqueValidate(field validator.FieldLevel) bool {
	permissions := &models.Permissions{}
	name := field.Field().String()
	param := field.Param()
	log.Println(param, "params")
	var id uint
	if param == "update" {
		i := field.Parent().Interface()
		request := i.(requrest.PermissionsUpdateRequest)
		id = request.ID
	}
	if permissions.CheckNameExist(name, id) {
		return false
	}
	return true
}

func PermissionsCodeUniqueValidate(field validator.FieldLevel) bool {
	permissions := &models.Permissions{}
	name := field.Field().String()
	param := field.Param()
	var id uint
	if param == "update" {
		i := field.Parent().Interface()
		request := i.(requrest.PermissionsUpdateRequest)
		id = request.ID
	}
	if permissions.CheckCodeExist(name, id) {
		return false
	}
	return true
}
